layui.define(['table', 'form', 'upload'], function(exports){
  var $ = layui.$,table = layui.table,form = layui.form,upload = layui.upload;

    $('#deviceTypeId').select2({
        placeholder: "请选择设备类型",
        width:'100%',
        theme: 'bootstrap',
        allowClear: true,
        ajax: {
            url: layui.setter.webRoot + '/device/type/select.data',
            method: 'post',
            delay: 250,
            processResults: function (result) {
                if (result.code == 200) {
                    return {
                        pagination: {more: result.body.more},
                        results: result.body.data || []
                    };
                } else {
                    return {results: []};
                }
            }
        }
    });

  table.render({
    elem: '#LAY-table'
    ,url: layui.setter.webRoot + '/device/produce/batch/list.data'
    ,method: 'post'
    ,parseData: function (res) {
        return {
            "code": res.code,
            "msg": res.msg,
            "count": res.body.total,
            "data": res.body.data
        };
    }
    ,response: {statusCode: 200}
    ,cols: [[
      {field: 'batchId', title: '批次号'}
      ,{field: 'name', title: '批次描述'}
      ,{field: 'deviceTypeId', title: '设备类型'}
      ,{field: 'startNum',title: '起始数位'}
      ,{field: 'produceTotal',title: '生产数量'}
      ,{field: 'stateName',title: '状态'}
      ,{field: 'createDate', title: '创建时间', sort: true}
      ,{title: '操作', width: 220, align:'center', fixed: 'right', templet:function(record){
          var html = '';
          if(record.state == 1){
              html = html + '<a class="layui-btn layui-btn-xs" lay-event="update">修改</a>';
              html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="createDevice">生成设备</a>';
          }else {
              html = html + '<a class="layui-btn layui-btn-xs" lay-event="info">查看</a>';
              if(record.state == 2) {
                  html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="createRelevance">生成关联设备</a>';
                  html = html + '<a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>';
              }else{
                  html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="deviceList">设备列表</a>';
                  html = html + '<a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="setBelong">设置归属</a>';
              }
          }
          return html;
      }}
    ]]
    ,page: true
    ,limit: 30
    ,height: 'full-220'
    ,text: '对不起，加载出现异常！'
  });
  
  //监听搜索
  form.on('submit(LAY-search)', function(data){
    var field = data.field;
    //执行重载
    table.reload('LAY-table', {
      where: field
      ,page: {curr: 1}
    });
  });
  
  //事件
  var active = {
      add:function(){
          layer.open({
              type: 2
              , title: '新增设备生产批次'
              , content: layui.setter.webRoot + '/device/produce/batch/add.html'
              , maxmin: false
              , area: ['500px', '500px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/device/produce/batch/add.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,update:function(obj){
          layer.open({
              type: 2
              , title: '修改设备生产批次'
              , content: layui.setter.webRoot + '/device/produce/batch/update.html?batchId='+obj.data.batchId
              , maxmin: false
              , area: ['500px', '500px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/device/produce/batch/update.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,createDevice:function(obj){
          layer.confirm('生成后批次将无法修改，确定要生成这批设备吗？', function(index){
              $.ajax({
                  type: "post",
                  url: layui.setter.webRoot + '/device/produce/batch/create.do',
                  data:{batchId:obj.data.batchId},
                  dataType:"json",
                  success:function(res){
                      if(res.code == 200){
                          layer.msg('生成完成',{icon: 1});
                          table.reload('LAY-table');
                      }else{
                          layer.msg(res.msg,{icon: 5});
                      }
                  }
              });
              layer.close(index);
          });
      }
      ,createRelevance:function (obj){
          layer.open({
              type: 2
              , title: '生产关联设备'
              , content: layui.setter.webRoot + '/device/produce/batch/relevance.html?batchId='+obj.data.batchId
              , maxmin: false
              , area: ['70%', '600px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/device/produce/batch/relevance.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('新增成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,setBelong:function (obj){
          layer.open({
              type: 2
              , title: '设置批次设备所属人'
              , content: layui.setter.webRoot + '/device/produce/batch/belong/set.html?batchId='+obj.data.batchId
              , maxmin: false
              , area: ['500px', '550px']
              , btn: ['确定', '取消']
              , yes: function(index, layero){
                  var iframeWindow = window['layui-layer-iframe'+ index]
                      ,submitID = 'LAY-form-submit'
                      ,submit = layero.find('iframe').contents().find('#'+ submitID);
                  iframeWindow.layui.form.on('submit('+ submitID +')', function(data){
                      var field = data.field;
                      $.ajax({
                          type: "post",
                          url: layui.setter.webRoot + '/device/produce/batch/belong/set.do',
                          data:field,
                          dataType:"json",
                          success:function(res){
                              if(res.code == 200){
                                  layer.close(index);
                                  table.reload('LAY-table');
                                  layer.msg('设置成功',{icon: 1});
                              }else{
                                  layer.msg(res.msg,{icon: 2});
                              }
                          }
                      });
                  });
                  submit.trigger('click');
              }
          });
      }
      ,info:function(obj) {
          layer.open({
              type: 2
              , title: '查看设备生产批次详情'
              , content: layui.setter.webRoot + '/device/produce/batch/info.html?batchId=' + obj.data.batchId
              , maxmin: true
              , area: ['90%', '90%']
          })
      }
      ,deviceList:function (obj){
          layer.open({
              type: 2
              , title: '查看批次设备列表'
              , content: layui.setter.webRoot + '/device/produce/batch/device/list.html?batchId=' + obj.data.batchId
              , maxmin: true
              , area: ['90%', '90%']
          })
      }
     ,del:function(obj){
    	layer.confirm('确定要删除该生产批次吗？', function(index){
	    	$.ajax({
	      	  type: "post",
	          url: layui.setter.webRoot + '/device/produce/batch/del.do',
	          data:{batchId:obj.data.batchId},
	  		  dataType:"json",
	  		  success:function(res){
	  			if(res.code == 200){
	  				obj.del();
	  			}else{
	  				layer.msg(res.msg,{icon: 5});
	  			}
	  		  }
	        });
	    	layer.close(index);
	    });
     }
     ,importExcel:function (){
          layer.open({
              type: 2
              , title: '导入设备信息'
              , content: layui.setter.webRoot + '/device/produce/batch/import.html'
              , maxmin: false
              , area: ['550px', '400px']
              , btn: ['确定', '取消']
              , yes: function (index, layero) {
                  var iframeWindow = window['layui-layer-iframe' + index]
                      , submit = layero.find('iframe').contents().find("#LAY-form-submit");
                  iframeWindow.layui.form.on('submit(LAY-form-submit)', function (data) {
                      var field = data.field;
                      console.log(field);
                      if (field.deviceTypeId == null || field.deviceTypeId == '') {
                          layer.msg("设备类型不能为空", {icon: 5});
                          return;
                      }
                      $('#uploadExcelPanel').html('<input type="file" name="file" id="uploadExcel"/>');
                      upload.render({
                          elem: '#uploadExcel'
                          , url: layui.setter.webRoot + '/device/produce/batch/import.do'
                          , data: field
                          , accept: 'file'
                          , before: function (obj) {
                              layer.load(); //上传loading
                          }
                          , done: function (res) {
                              if (res.code == 200) {
                                  layer.msg("导入成功", {icon: 1});
                                  table.reload('LAY-table');
                                  layer.close(index);
                              } else {
                                  layer.msg(res.msg, {icon: 5});
                              }
                              layer.closeAll('loading'); //关闭loading
                          }
                          , error: function (res) {
                              //请求异常回调
                              layer.closeAll('loading');
                              layer.msg(res.msg, {icon: 5});
                          }
                      });
                      console.log("------")
                      $('#uploadExcel').click();
                  });
                  submit.trigger('click');
              }
          });
      }
  };
  
  $('.layui-btn.LAY-btn-fun').on('click', function(){
    var type = $(this).data('type');
    active[type] ? active[type].call(this) : '';
  });
  
  table.on('tool(LAY-table)', function(obj){
  	var type = obj.event ;
   	active[type] ? active[type].call(this,obj) : '';
  });
  
  exports('device_produce_batch_list', {})
});